package com.ruoyi.common.config.mybatis.method;

import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * @Classname DataRecoveryByIds
 * @Author: sunluquan
 * @Date: 2021/12/23 15:54
 * @Description TODO
 * 恢复删除标记的数据为正常  根据ids
 */
public class RecoveryBatchByIds extends AbstractLogicRecoveryMethod {

    /**
     *
     * @param mapperClass
     * @param modelClass
     * @param tableInfo
     * @return
     */
    @Override
    public MappedStatement reachableInjectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {

        String sql;
        SqlMethod sqlMethod = SqlMethod.RECOVER_BATCH_BY_IDS;
        sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlLogicNotDeleteSet(tableInfo),
                tableInfo.getKeyColumn(),
                SqlScriptUtils.convertForeach(
                        SqlScriptUtils.convertChoose("@org.apache.ibatis.type.SimpleTypeRegistry@isSimpleType(item.getClass())",
                                "#{item}", "#{item." + tableInfo.getKeyProperty() + "}"),
                        COLLECTION, null, "item", COMMA),
                        sqlLogicDeleteWhere(tableInfo));
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Object.class);
        return addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod() , sqlSource);
    }
}
